#
#


import os
import logging
import sys
from paste import deploy
from wsgiref.simple_server import make_server

logging.basicConfig()
LOG = logging.getLogger(__name__)

module_dir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
                        os.pardir,os.pardir))

sys.path.insert(0,module_dir)
print sys.path

bind_host = "127.0.0.1"
bind_port = 8080

print "sys.argv[0]) %s, module dir is %s" % (sys.argv[0],module_dir)
#print "os.path.join(os.path.abspath(sys.argv[0]),os.pardir,os.pardir) %s" % os.path.join(os.path.abspath(sys.argv[0]),os.pardir,os.pardir)

def load_paste_app(app_name, conf_file):
    LOG.debug("Loading %(app_name) from %(conf_file)",
                {'app_name':app_name, 'conf_file':conf_file})
    
    try:
        app = deploy.loadapp("config:%s" % os.path.abspath(conf_file), name=app_name)
        return app
    except (LookupError, ImportError) as e:
        LOG.error(str(e))
        raise RuntimeError(str(e))



def server(app_name, conf_file):
    app = load_paste_app(app_name, conf_file)
    serve = make_server(bind_host, bind_port, app)
    serve.serve_forever() 


    
if __name__ == '__main__':
    #app_name = "zhwsgi_1"
    app_name = "zhwsgi_2"
    conf_file = "zhwsgi.ini"
    server(app_name, conf_file)



